from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone


class Software(models.Model):
    name = models.CharField(max_length=200, verbose_name="软件名称")
    vendor = models.CharField(max_length=200, blank=True, verbose_name="厂商")
    category = models.CharField(max_length=100, blank=True, verbose_name="类别")
    description = models.TextField(blank=True, verbose_name="描述")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "软件"
        verbose_name_plural = "软件"
        ordering = ["name"]

    def __str__(self):
        return self.name


class SoftwareLicense(models.Model):
    STATUS_CHOICES = [
        ("active", "有效"),
        ("expired", "已过期"),
        ("revoked", "已回收"),
    ]
    software = models.ForeignKey(Software, on_delete=models.CASCADE, related_name="licenses", verbose_name="软件")
    license_key = models.CharField(max_length=255, blank=True, verbose_name="授权密钥")
    seats = models.PositiveIntegerField(default=1, verbose_name="许可数量")
    start_date = models.DateField(null=True, blank=True, verbose_name="开始日期")
    end_date = models.DateField(null=True, blank=True, verbose_name="结束日期")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="active", verbose_name="状态")
    supplier = models.ForeignKey('assets.Supplier', on_delete=models.SET_NULL, null=True, blank=True, verbose_name="供应商")
    notes = models.TextField(blank=True, verbose_name="备注")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "软件许可"
        verbose_name_plural = "软件许可"
        ordering = ["-created_at"]

    def __str__(self):
        return f"{self.software.name} - {self.license_key or 'N/A'}"

    @property
    def is_expired(self):
        return bool(self.end_date and timezone.now().date() > self.end_date)


class SoftwareInstallation(models.Model):
    license = models.ForeignKey(SoftwareLicense, on_delete=models.CASCADE, related_name="installations", verbose_name="许可")
    asset = models.ForeignKey('assets.Asset', on_delete=models.SET_NULL, null=True, blank=True, verbose_name="资产")
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="用户")
    department = models.ForeignKey('assets.Department', on_delete=models.SET_NULL, null=True, blank=True, verbose_name="部门")
    installed_at = models.DateTimeField(default=timezone.now, verbose_name="安装时间")
    uninstalled_at = models.DateTimeField(null=True, blank=True, verbose_name="卸载时间")
    notes = models.TextField(blank=True, verbose_name="备注")

    class Meta:
        verbose_name = "软件安装"
        verbose_name_plural = "软件安装"
        ordering = ["-installed_at"]

    def __str__(self):
        target = self.asset or self.user or self.department
        return f"{self.license} -> {target}"
